<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button v-auth="'SysGen.Read'" type="primary" plain icon="el-icon-reading" :disabled="selection.length==0" @click="open_perm">权限</el-button>
				<el-button v-auth="'SysGen.Read'" type="primary" plain icon="el-icon-menu" :disabled="selection.length==0" @click="open_func">功能</el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="name" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="表名称" prop="name" min-width="200"></el-table-column>
				<el-table-column label="表描述" prop="remarks" min-width="200"></el-table-column>
                <el-table-column label="操作" fixed="right" align="right" width="120">
					<template #default="scope">
						<el-button-group>
							<el-link v-auth="'SysGen.Read'" type="primary" size="small" @click="downFile(scope.row)" icon="el-icon-download">视图下载</el-link>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<v-dialog v-if="dialog.v" ref="vDialog" @closed="dialog.v=false"></v-dialog>

</template>

<script>
	import setting from './setting'
	// import http from "@/utils/request"	
	import vDialog from './v'

	export default {
		name: 'gen',
		components: {
			vDialog
		},
		data() {
			return {
				dialog: {
					v: false
				},
				apiObj: setting.api.list,
				selection: [],
				search: {
					id: null
				},
				scPageSize: 100
			}
		},
		methods: {
			open_perm(){
				const names = this.selection.map(item=>item.name).join(',')
				this.dialog.v = true
				this.$nextTick(() => {
					this.$refs.vDialog.open('show').setData(names,'perm.sql')
				})
			},
			open_func(){
				const names = this.selection.map(item=>item.name).join(',')
				this.dialog.v = true
				this.$nextTick(() => {
					this.$refs.vDialog.open('show').setData(names,'func.sql')
				})
			},
			selectionChange(selection){
				this.selection = selection;
			},
			async downFile(row) {
				let res = await setting.api.token.get({seconds: 300})
				if (res.code == 0) {
					location.href = setting.api.down.url + '?view=list.vue,save.vue,setting.js&table=' + row.name + "&TOKEN=" + res.data.token
				} else {
					this.$alert(res.msg, "提示", {type: 'error'})
					return false
				}
								

				// let d ={
				// 	"table": row.name,
				// 	"view": "list.vue,save.vue,setting.js"
				// }
				// let u = setting.api.down.url
				// let c = {responseType:"blob"}
				// http.get(u,d,c).then(res=>{
				// 	let link = document.createElement("a");
				// 	const blob = new Blob([res], {
				// 		type: "application/zip"
				// 	});
				// 	link.href = URL.createObjectURL(blob);
				// 	link.setAttribute("download", row.name + ".zip");
				// 	link.click();
				// 	link = null;
				// }).catch((error)=>{
				// 	if (error) {
				// 		this.$alert(error, "提示", {type: 'error'})
				// 	} else {
				// 		this.$alert("下载失败", "提示", {type: 'error'})
				// 	}
				// })
			}
		}
	}
</script>

<style>
</style>
